//////////////////////////////////////////////////////////////////////
//!\file MainWindow.hpp
//!\brief Main Application.
//!\author timmsusa@googlemail.com
//////////////////////////////////////////////////////////////////////

#ifndef MAINWINDOW_HPP
#define MAINWINDOW_HPP

#include <QWidget>
#include <QMainWindow>
#include <QtGui>
#include "FileSystemModel.hpp"
#include <QTreeView>
#include <QFileInfo>
#include <QDebug>
#include <QTableWidget>
#include <QProgressDialog>
#include "StatusBar.hpp"
#include "FlacTrack.hpp"
#include "FlacMetaDataManager.hpp"
#include "FileSystemManager.hpp"
#include "Persistence.hpp"
#include "ConfigDialog.hpp"


class MainWindow : public QMainWindow, public FlacMetadataManager
{
    Q_OBJECT

public:
    MainWindow( QWidget *parent = 0 );
    ~MainWindow();

public slots:
    void onSetMusicCollectionPath();
    void onSetFileIncomingPath();
    void onShowSettingsMenu();
    void onShowUpdateFolderTree(const QVector < Album >& list);
    void onClickedData( const QModelIndex & index ); // When clicked on left Tree View
    void onDblClickedData( const QModelIndex & index );
    void onCollectionTreeCtxMenu( QPoint );
    void onUpdateTreeClickedIdx(QModelIndex idx); // right
    void onUpdateTreeDblClicked(QModelIndex);
    void onTreeUpdateCtxMenu(QPoint);
    void onTreeUpdateCtxMenuBottom(QPoint);
    void slotFlacFound(QString file);
    void slotSearchInMusicCollectionFinished();
    void slotSearchInFileIncomingFinished();
    void slotFlacListComparisonFinished(QVector < Album > list);
    void slotCopyToMusicCollection();
    void slotCopyFromUpdateViewBottomToMusicCollection();
    void slotCopyProgress(int cnt, QString path);
    void slotCopyProgressFinished(bool wasSuccessful);
    void slotRemoveProgress(int cnt, QString path);
    void slotRemoveProgressFinished(bool wasSuccessful);
    void slotRemoveFromCollection();
    void slotRescanAll();
    void slotSetItemToCollectionRootPath();
    void slotGoToLastCollectionRootPath();
    void slotCreateDirInCollection();
    void slotRenameDirInCollection();
    void slotRenamedDirPressedEnter();
    void slotEnableWizard(bool enable);
    void slotEnableDeepScan(bool enable);
    void slotFlacFolderFound(Album album);
    void slotRunSelectCollectionPathMsgBox();
    void slotRescanButtonClicked();

signals:   
    void sigSearchInMusicCollection(QString path);
    void sigSearchInFileIncoming(QString path);
    void sigCompareFlacLists();
    void sigCopy(bool isFile, QString source, QString dest);
    void sigCopyAllFolderTo(QStringList srcPaths, QStringList destPath);
    void sigResetCopyProgressCounter();
    void sigResetRemoveProgressCounter();
    void sigRescanAll();
    void sigRemoveFromFileSystem(QStringList pathList);
    void sigEnableDeepScan(bool enable);
    void sigRunSelectCollectionPathMsgBox();
    void sigStopAllSearch();

private:
    // Private methods
    void createMainGui();
    void createFileSystemManagerThread();
    bool runMsgBoxRemoveSelection();
    bool runMsgBoxFinishedFlacSearchCollection();
    bool runMsgBoxSelectCollectionPath();
    bool runMsgBoxFolderExistsOrCopy();
    bool runMsgBoxIncomingPathContainedCollectionPath();
    void setTableLabels(int count);
    void setTreeViewRoot(const QString& path);
    void fillMetavalueTable();
    void putFlacMetaDataToTable(const QVector< QString >& metadataValues, bool isFile);
    void traverseFileInfoList(QVector< QString >& metadataValues);
    QFileInfoList getSelectionListFromCollectionTreeView()const;
    void removeCurFileInfoContent();
    void removeSelectionFromCollection();
    void removeContentFromFileInfoList(const QFileInfoList& fileInfoList);
    void copySelectionFromUpdateCollectionTop();
    void copySelectionFromUpdateCollectionBottom();
    const QFileInfo& getFileInfoByUpdateTreeTopModelIdx(const QModelIndex& idx);
    const QFileInfo& getFileInfoByUpdateTreeBottomModelIdx(const QModelIndex& idx);
    void removeSelectedUpdateItems();
    void addAlbumTracksToParentItem(const Album& album, QStandardItem* parentItem);
    void clearUpdateViews();
    void readPersistedFilePath();
    void handleStartBehaviour();
    void quitFileSystemManagerThread();


    // Membervariables
    Persistence m_Persistence;
    FileSystemManager m_FileSystemManager;
    FileSystemModel m_FSModel;
    QTableWidget *m_pTable;
    QFileInfo m_CurFileInfo;
    QFileInfoList m_CurFileInfoList;
    QStringList m_CollectionPathHistory;
    QTreeView *m_pTreeUpdateViewTop;
    QTreeView *m_pTreeUpdateViewBottom;
    QStandardItemModel *m_pUpdateModelTop;
    QStandardItemModel *m_pUpdateModelBottom;
    QModelIndex m_UpdateTreeViewTModelIdx;
    QModelIndex m_UpdateTreeViewBModelIdx;
    QModelIndexList m_UpdateTreeViewTModelIdxList;
    QModelIndexList m_UpdateTreeViewBModelIdxList;
    StatusBar *m_pStatusBar;
    ConfigDialog m_ConfigDialog;
    QProgressDialog m_ProgressDialog;

    QTreeView* m_pTree;
    QMenu *m_pCollectionCtxMenu;
    QMenu *m_pUpdateTreeCtxMenuTop;
    QMenu *m_pUpdateTreeCtxMenuBottom;
    QLineEdit *m_LineEditCollection;

    QString m_MusicCollectionPath;
    QString m_FileIncomingPath;

    QStringList m_CopyRequestSrcList;
    QStringList m_CopyRequestDestList;
    bool        m_IsCopyRequested;
    int         m_LastCopyRequestedFileCount;

    bool m_IsFile;
    int m_TableRow;
    int m_TableColoumn;
    bool m_WizzardEnabled;
    bool m_DeepScanEnabled;
};

#endif // MAINWINDOW_HPP
